<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
        canvas {
            border: 1px solid black;
            background-color: #f0f0f0;
        }
    </style>
</head>

<body>
    <!-- 
      1.在HTML页面中添加Canvas元素
      2.设置Canvas的尺寸和样式
      3.另一种是在 JavaScript 中通过设置 canvas 对象的 width 和 height 属性来实现
      -->
    <canvas id="myCanvas" width="400" height="300"></canvas>
    <script>
        var canvas = document.getElementById("myCanvas");
        var ctx = canvas.getContext("2d");
        var x = 0;
        var intervalId = setInterval(function() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            ctx.fillRect(x, 50, 50, 50);
            x += 5;
            if (x > canvas.width) {
                clearInterval(intervalId);
            }
        }, 50);
    </script>
</body>

</html>